Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unit tests #22

Merged
merged 2 commits into from
Sep 26, 2023
Merged

feat: add unit tests #22

merged 2 commits into from
Sep 26, 2023

Conversation

mkindahl
Copy link
Collaborator

Add unit tests and unit test framework for existing functionality.

@mkindahl mkindahl force-pushed the add-unittest branch 11 times, most recently from 570f000 to d4f64c1 Compare September 20, 2023 11:56
@CLAassistant
Copy link

CLAassistant commented Sep 21, 2023

CLA assistant check
All committers have signed the CLA.

@mkindahl mkindahl force-pushed the add-unittest branch 3 times, most recently from b7dd79f to 18953c8 Compare September 21, 2023 21:57
@mkindahl mkindahl self-assigned this Sep 24, 2023
Copy link
Member

@JamesGuthrie JamesGuthrie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I would like to propose that instead of running the test container in the github action, it's started by the test setup system, using something like testcontainers. This reduces a hurdle in running the tests locally.

The following diff implements this:

diff --git a/src/doctor/rules/compression_test.py b/src/doctor/rules/compression_test.py
index b74d4a7..9b8f381 100644
--- a/src/doctor/rules/compression_test.py
+++ b/src/doctor/rules/compression_test.py
@@ -19,6 +19,7 @@ import unittest
 import psycopg2

 from psycopg2.extras import RealDictCursor
+from testcontainers.postgres import PostgresContainer
 from timescaledb import Hypertable

 from doctor.rules.compression import LinearSegmentBy, PointlessSegmentBy
@@ -34,15 +35,10 @@ class TestCompressionRules(unittest.TestCase):

     def setUp(self):
         """Set up unit tests for compression rules."""
-        user = os.getenv("PGUSER")
-        host = os.getenv("PGHOST")
-        port = os.getenv("PGPORT") or "5432"
-        dbname = os.getenv("PGDATABASE")
-        password = os.getenv("PGPASSWORD")
-        print(f"connecting to {host}:{port} database {dbname}")
-        self.__conn = psycopg2.connect(dbname=dbname, user=user, host=host,
-                                       password=password, port=port,
-                                       cursor_factory=RealDictCursor)
+        self.__container = PostgresContainer("timescale/timescaledb-ha:pg15", driver="default").start()
+        connstring = self.__container.get_connection_url().replace("+psycopg2", "")
+        print(f"connecting to {connstring}")
+        self.__conn = psycopg2.connect(connstring, cursor_factory=RealDictCursor)
         table = Hypertable("conditions", "time", {
                            'time': "timestamptz not null",
                            'device_id': "integer",
@@ -71,6 +67,7 @@ class TestCompressionRules(unittest.TestCase):
         with self.__conn.cursor() as cursor:
             cursor.execute("DROP TABLE conditions")
         self.__conn.commit()
+        self.__container.stop()

     def run_rule(self, rule):
         """Run rule and return messages."""

@mkindahl
Copy link
Collaborator Author

LGTM.

I would like to propose that instead of running the test container in the github action, it's started by the test setup system, using something like testcontainers. This reduces a hurdle in running the tests locally.

The following diff implements this:

diff --git a/src/doctor/rules/compression_test.py b/src/doctor/rules/compression_test.py
index b74d4a7..9b8f381 100644
--- a/src/doctor/rules/compression_test.py
+++ b/src/doctor/rules/compression_test.py
@@ -19,6 +19,7 @@ import unittest
 import psycopg2

 from psycopg2.extras import RealDictCursor
+from testcontainers.postgres import PostgresContainer
 from timescaledb import Hypertable

 from doctor.rules.compression import LinearSegmentBy, PointlessSegmentBy
@@ -34,15 +35,10 @@ class TestCompressionRules(unittest.TestCase):

     def setUp(self):
         """Set up unit tests for compression rules."""
-        user = os.getenv("PGUSER")
-        host = os.getenv("PGHOST")
-        port = os.getenv("PGPORT") or "5432"
-        dbname = os.getenv("PGDATABASE")
-        password = os.getenv("PGPASSWORD")
-        print(f"connecting to {host}:{port} database {dbname}")
-        self.__conn = psycopg2.connect(dbname=dbname, user=user, host=host,
-                                       password=password, port=port,
-                                       cursor_factory=RealDictCursor)
+        self.__container = PostgresContainer("timescale/timescaledb-ha:pg15", driver="default").start()
+        connstring = self.__container.get_connection_url().replace("+psycopg2", "")
+        print(f"connecting to {connstring}")
+        self.__conn = psycopg2.connect(connstring, cursor_factory=RealDictCursor)
         table = Hypertable("conditions", "time", {
                            'time': "timestamptz not null",
                            'device_id': "integer",
@@ -71,6 +67,7 @@ class TestCompressionRules(unittest.TestCase):
         with self.__conn.cursor() as cursor:
             cursor.execute("DROP TABLE conditions")
         self.__conn.commit()
+        self.__container.stop()

     def run_rule(self, rule):
         """Run rule and return messages."""

Seems reasonable to do, but then it might be useful to allow each test to pick the container. Some of the rules only works if you have a container that do not have TimescaleDB installed, so to check those, you need to pick a standard PostgreSQL image.

Tempted to push this and handle that as a separate PR, but going to investigate if this is straightforward to do and include in this PR.

Add unit tests and unit test framework for existing functionality.
@mkindahl
Copy link
Collaborator Author

Creating a good container support for test cases was more work then expected. Pushing this and taking this as a separate PR.

@mkindahl mkindahl merged commit 31f4826 into timescale:main Sep 26, 2023
@mkindahl mkindahl deleted the add-unittest branch September 26, 2023 08:00
@mkindahl mkindahl linked an issue Sep 26, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add unit tests for rules
3 participants